#include "LinkNode.h"
template <typename T>
LinkNode<T>::LinkNode() {
	this->element = T();
	this->next = nullptr;
}
template <typename T>
LinkNode<T>::LinkNode(T element) {
	this->element = element;
	this->next = nullptr;
}
template <typename T>
LinkNode<T>::~LinkNode() {
	
}


// 链表长度
template <typename T>
int LinkList<T>::size() {
	int len = 0;
	LinkNode<T>* p = this->head;
	while (p->next != nullptr) {
		p = p->next;
		len++;
	}
	return len;
}
// 判断空
template <typename T>
bool LinkList<T>::isEmpty() {
	if (this->head->next == nullptr)	return true;
	else    return false;
}
// 插入elem
template <typename T>
bool LinkList<T>::insert(T element,int pos) {
	if (pos<0 || pos>this->size())	return false;
	else {
		LinkNode<T>* p = this->head;
		for (int i = 0; i < pos; i++) {
			p = p->next;
		}
		LinkNode<T>* elem = new LinkNode<T>(element);
		elem->next = p->next;
		p->next = elem;
		return true;
	}
}
// 清空链表
template <typename T>
void LinkList<T>::clear() {
	LinkNode<T>* p = this->head,*r;
	while (p->next != nullptr) {
		r = p->next;
		p->next = r->next;
		free(r);
	}
	this->head->next = nullptr;
}
// 获取指定位置的elem
template <typename T>
T* LinkList<T>::getElement(int pos) {
	if (pos<0 || pos>this->size())	return nullptr;
	else {
		LinkNode<T>* p = this->head;
		for (int i = 0; i < pos+1; i++) {
			p = p->next;
		}
		return &(p->element);
	}
}
// 设置制定位置的elem
template <typename T>
bool LinkList<T>::setElement(T elem,int pos) {
	if (pos<0 || pos>=this->size())	return false;
	else {
		LinkNode<T>* p = this->getNode(pos);
		p->element = elem;
		return true;
	}
}
// 获取指定位置的节点
template <typename T>
LinkNode<T>* LinkList<T>::getNode(int pos) {
	if (pos<0 || pos>this->size())	return nullptr;
	else {
		LinkNode<T>* p = this->head;
		for (int i = 0; i < pos + 1; i++) {
			p = p->next;
		}
		return p;
	}
}
template <typename T>
LinkList<T>::LinkList() {
	this->head = new LinkNode<T>();
	head->next = nullptr;
}
template <typename T>
LinkList<T>::~LinkList() {
	clear();
}